#include "Simon.h"
#include "Resource.h"


/**--------------------------------------------------------
*Description : Constructor
*Method name : Mario
*Parameters :  float x, float y, bool isAi
*Return value :
--------------------------------------------------------**/
Simon::Simon(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 backBuffer, LPD3DXSPRITE spriteHandler, float x, float y, CastleVaniaGame* Game_Mario, int Obj_state, int Direction)
{
	this->game_main = Game_Mario;

	this->d3ddv = d3ddv;
	this->backBuffer = backBuffer;
	this->spriteHandler = spriteHandler;

	//p_spriteSmall = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\mario_small.png"), 125, 0);
	//p_spriteSmall = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\player.png"), 125, 0);
	p_sprite_go = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[1]->getFileName()), 125, 0);

	/*p_spriteBig = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\mario_big.png"), 125, 0);
	p_spriteBullet = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\mario_bullet.png"), 125, 0);
	p_spriteLight = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\mario_small_light.png"), 125, 0);*/
	p_sprite = p_sprite_go;

	//Castle	
	p_sprite_player_rod = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[2]->getFileName()), 125, 0);
	p_sprite_stair = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[13]->getFileName()), 125, 0);
	p_sprite_die = new Sprite(spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[46]->getFileName()), 125, 0);


	is_turn_left = false;
	is_jump = false;

	//isSmall = true;			//mario con nho
	isShoot = false;		//chua co bullet
	isHasStar = false;		//chua co sao ba dao
	isMoveDown = false;		//chua chuoi cong
	isGravity = true;		//co Trong luc
	isDieFall = false;

	//Castlevania
	isFight = false;
	countFrameRod = 3;


	is_going_stair = false;
	is_until_go_out_stair = false;



	isUp = 0;	//0: bt, -1:di xuong, 1: di len

	x_temp = 0;
	y_temp = 0;

	is_active_axe = false;

	rod_version = 1;
	blood = 8;	//max = 8

	weapon_version = WEAPON_NONE;		//chua co vu khi
	//



	pos_x = x;
	pos_y = y;

	pos_x_old = x;
	pos_y_old = y;

	veloc_x = 0;
	veloc_y = 0;
	accel_x = 0;
	accel_y = 0;


	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();
	const_width = width;

	timeBullet = 0;
	timeTransferState = 0;
	countFrame = 0;

	//bullet = NULL;
	//bullet2 = NULL;
	p_rod = NULL;
	p_axe = NULL;
	p_dagger = NULL;
	p_holy_water = NULL;
	p_boomerang = NULL;


	this->obj_state = Obj_state;
	this->direction = Direction;
	if (obj_state == MS_NORMAL)
	{
		if (direction == 1)			//mario nho
		{
			p_sprite = p_sprite_go;
			//isSmall = true;
		} 
		else if (direction == 21)	//mario to
		{
			//p_sprite = p_spriteBig;
			//isSmall = false;
		}
		//else if (direction == 22)	//mario to, co bullet
		//{
		//	p_sprite = p_spriteBullet;
		//	//isSmall = false;
		//	isShoot = true;
		//}
	} 
	else if (obj_state == MS_HASSTAR)
	{
		timeProtect = 0;
		countFrame = 0;
		isHasStar = true;
		if (direction == 1)			//mario nho
		{
			p_sprite = p_sprite_go;
			//isSmall = true;
		} 
		else if (direction == 21)	//mario to
		{
			//p_sprite = p_spriteBig;
			//isSmall = false;
		}
		//else if (direction == 22)	//mario to, co bullet
		//{
		//	p_sprite = p_spriteBullet;
		//	//isSmall = false;
		//	isShoot = true;
		//}
	}
	else if (obj_state == MS_PROTECT)
	{
		timeProtect = 0;
		countFrame = 0;

		isHasStar = false;
		if (direction == 1)			//mario nho
		{
			p_sprite = p_sprite_go;
			//isSmall = true;
		} 
		else if (direction == 21)	//mario to
		{
			//p_sprite = p_spriteBig;
			//isSmall = false;
		}
		//else if (direction == 22)	//mario to, co bullet
		//{
		//	p_sprite = p_spriteBullet;
		//	//isSmall = false;
		//	isShoot = true;
		//}
	}
	else if (obj_state == MS_DIE)
	{
		p_sprite = p_sprite_go;
		//if (_turnLeft == true)
		if (direction == 1)		//left
		{
			p_sprite->setFrame(6,6);
		} 
		else if (direction == 2)	//right
		{
			p_sprite->setFrame(4,4);
		}
		veloc_y = 1.5;
		accel_y = GRAVITY;
	} 
	//else if (obj_state == MS_SMALLTOBIG)
	//{
	//	//_y = _y + 30;
	//	//isSmall = false;
	//	timeTransferState = 0;
	//	countFrame = 0;
	//	p_sprite = p_sprite_go;
	//	//if (_turnLeft == true)
	//	if (direction == 1)		//left
	//	{
	//		is_turn_left = true;
	//		//_sprite->SetFrame(9, 11);
	//	} 
	//	else if (direction == 2)	//right
	//	{
	//		is_turn_left = false;
	//		//_sprite->SetFrame(0, 2);
	//	}
	//}
	//else if (obj_state == MS_BIGTOSMALL)
	//{
	//	pos_y = pos_y - 30;
	//	//isSmall = true;
	//	timeTransferState = 0;
	//	countFrame = 0;
	//	//p_sprite = p_spriteBig;
	//	//if (_turnLeft == true)
	//	if (direction == 1)		//left
	//	{
	//		is_turn_left = true;
	//		//_sprite->SetFrame(9, 11);
	//	} 
	//	else if (direction == 2)	//right
	//	{
	//		is_turn_left = false;
	//		//_sprite->SetFrame(0, 2);
	//	}
	//}
	//else if (obj_state == MS_BULLETTOSMALL)
	//{
	//	pos_y = pos_y - 30;
	//	//isSmall = true;
	//	isShoot = false;
	//	timeTransferState = 0;
	//	countFrame = 0;
	//	//p_sprite = p_spriteBig;
	//	//if (_turnLeft == true)
	//	if (direction == 1)		//left
	//	{
	//		is_turn_left = true;
	//		//_sprite->SetFrame(9, 11);
	//	} 
	//	else if (direction == 2)	//right
	//	{
	//		is_turn_left = false;
	//		//_sprite->SetFrame(0, 2);
	//	}
	//}
	//else if (obj_state == MS_BIGTOBULLET)
	//{
	//	//isSmall = false;
	//	isShoot = true;
	//	timeTransferState = 0;
	//	countFrame = 0;
	//	//p_sprite = p_spriteBig;
	//	//if (_turnLeft == true)
	//	if (direction == 1)		//left
	//	{
	//		is_turn_left = true;
	//		//_sprite->SetFrame(9, 11);
	//	} 
	//	else if (direction == 2)	//right
	//	{
	//		is_turn_left = false;
	//		//_sprite->SetFrame(0, 2);
	//	}
	//}


}


/**--------------------------------------------------------
*Description : Destructor
*Method name : Mario
*Parameters :   
*Return value :
--------------------------------------------------------**/
Simon::~Simon(void)
{
	if (p_sprite)
	{
		delete p_sprite;
	}

	/*if (bullet)
	{
	delete bullet;
	}
	if (bullet2)
	{
	delete bullet2;
	}*/
	if (p_rod)
	{
		delete p_rod;
	}
	if (p_axe)
	{
		delete p_axe;
	}
	if (p_dagger)
	{
		delete p_dagger;
	}
	if (p_holy_water)
	{
		delete p_holy_water;
	}
	if (p_boomerang)
	{
		delete p_boomerang;
	}
}


/**--------------------------------------------------------
*Description : Update mario theo thoi gian
*Method name : Update
*Parameters :  int time
*Return value :void
--------------------------------------------------------**/


void Simon::update(list<Object*>* obj_list, DxInput* input, float time)
{
	switch (this->obj_state)
	{
	case MS_NORMAL:
		{
			if (is_turn_left == true)
			{
				direction = 1;
			}
			else
			{
				direction = 2;
			}

			//Update Input
			this->processInput(input);
			//Update move
			this->move(obj_list, time, input);

			if (p_rod != NULL)
			{
				p_rod->updatePosition(obj_list, input, time, this->pos_x, this->pos_y);
				if (p_rod->num_score != 0)
				{
					game_main->num_score += p_rod->num_score;
					p_rod->num_score = 0;
					game_main->scoreArr[SCORE]->Update(game_main->num_score);
				}
			}

			if (p_axe != NULL)
			{
				p_axe->update(obj_list, input, time);
			}
			if (p_dagger != NULL)
			{
				p_dagger->update(obj_list, input, time);
			}
			if (p_holy_water != NULL)
			{
				p_holy_water->update(obj_list, input, time);
			}
			if (p_boomerang != NULL)
			{
				p_boomerang->update(obj_list, input, time);
			}
		}
		break;

	case MS_BLOOD_LOOS:
		//Save map
		if (is_turn_left == true)
		{
			direction = 1;
		}
		else
		{
			direction = 2;
		}
		this->moveDieState(obj_list, time, input);
		break;

	case MS_DIE:
		//Save map
		if (is_turn_left == true)
		{
			direction = 1;
		}
		else
		{
			direction = 2;
		}
		timeProtect += time;
		if (timeProtect > 100)
		{
			timeProtect = 0;
			countFrame++;			
			if (countFrame > 29)
			{
				game_main->ChangeState(GS_REPLAY);
			}
		}
		break;

	case MS_LIGHT:
		if (is_turn_left == true)
		{
			direction = 1;
		} 
		else
		{
			direction = 2;
		}

		timeProtect += time;
		if (timeProtect > 40)
		{
			timeProtect = 0;
			countFrame++;			
			if (countFrame > 19)
			{
				p_sprite = p_sprite_go;
				changeState(MS_NORMAL);
			}
		}
		break;

	case MS_PROTECT:
		{
			if (is_turn_left == true)
			{
				direction = 1;
			} 
			else
			{
				direction = 2;
			}
			timeProtect += time;
			if (timeProtect > 40)
			{
				timeProtect = 0;
				countFrame++;			
				if (countFrame > 29)
				{
					p_sprite = p_sprite_go;
					changeState(MS_NORMAL);
				}
			}
			/*processInput(input);
			move(obj_list, time, input);*/
		}
		break;

		//////////////////////////////////////////////////////////////////////////
	//case MS_CREPTPINE:
	//	if (is_turn_left == true)
	//	{
	//		direction = 1;
	//	} 
	//	else
	//	{
	//		direction = 2;
	//	}

	//	veloc_y -= accel_y * time;
	//	pos_y += veloc_y * time + 1/2 * accel_y * time *time;
	//	if (pos_y <= pos_y_old - 64)
	//	{
	//		//changeState(MS_REMOVE);
	//		changeState(MS_NORMAL);
	//		game_main->ChangeState(GS_SUB_STAGE);
	//	}		
	//	break;

	//case MS_SMALLTOBIG:
	//	if (is_turn_left == true)
	//	{
	//		direction = 1;
	//	} 
	//	else
	//	{
	//		direction = 2;
	//	}
	//	timeTransferState += time;
	//	if (timeTransferState > 40)
	//	{
	//		timeTransferState = 0;
	//		countFrame++;
	//		if (countFrame % 2 != 0)		//frame le
	//		{
	//			pos_y = pos_y + 30;
	//			//p_sprite = p_spriteBig;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(8, 9);
	//			}
	//			else
	//			{
	//				p_sprite->setFrame(1, 2);
	//			}
	//		}
	//		else
	//		{
	//			pos_y = pos_y - 30;
	//			p_sprite = p_sprite_go;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(10,11);
	//			} 
	//			else
	//			{
	//				p_sprite->setFrame(0,1);
	//			}
	//		}
	//		if (countFrame > 8)
	//		{
	//			//ChangeSprite(_spriteBig);
	//			//p_sprite = p_spriteBig;
	//			changeState(MS_NORMAL);
	//		}
	//	}
	//	break;


	//case MS_BIGTOSMALL:
	//	if (is_turn_left == true)
	//	{
	//		direction = 1;
	//	} 
	//	else
	//	{
	//		direction = 2;
	//	}
	//	timeTransferState += time;
	//	if (timeTransferState > 40)
	//	{
	//		timeTransferState = 0;
	//		countFrame++;
	//		if (countFrame % 2 != 0)		//frame le
	//		{
	//			pos_y = pos_y - 30;
	//			p_sprite = p_sprite_go;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(10,11);
	//			} 
	//			else
	//			{
	//				p_sprite->setFrame(0,1);
	//			}
	//		}
	//		else								//frame chan
	//		{
	//			pos_y = pos_y + 30;
	//			//p_sprite = p_spriteBig;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(8,9);
	//			} 
	//			else
	//			{
	//				p_sprite->setFrame(1,2);
	//			}

	//		}
	//		/*_xOld = pos_x;
	//		_yOld = pos_y;*/
	//		if (countFrame > 8)
	//		{				
	//			p_sprite = p_sprite_go;
	//			changeState(MS_PROTECT);
	//		}
	//	}
	//	break;

	//case MS_BULLETTOSMALL:
	//	if (is_turn_left == true)
	//	{
	//		direction = 1;
	//	} 
	//	else
	//	{
	//		direction = 2;
	//	}
	//	timeTransferState += time;
	//	if (timeTransferState > 40)
	//	{
	//		timeTransferState = 0;
	//		countFrame++;
	//		if (countFrame % 2 != 0)		//frame le
	//		{
	//			pos_y = pos_y - 30;
	//			p_sprite = p_spriteSmall;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(10,11);
	//			} 
	//			else
	//			{
	//				p_sprite->setFrame(0,1);
	//			}
	//		}
	//		else								//frame chan
	//		{
	//			pos_y = pos_y + 30;
	//			p_sprite = p_spriteBullet;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(8,9);
	//			} 
	//			else
	//			{
	//				p_sprite->setFrame(1,2);
	//			}

	//		}
	//		if (countFrame > 8)
	//		{
	//			p_sprite = p_spriteSmall;
	//			changeState(MS_PROTECT);
	//		}
	//	}
	//	/*_xOld = pos_x;
	//	_yOld = pos_y;*/
	//	break;

	//case MS_BIGTOBULLET:
	//	if (is_turn_left == true)
	//	{
	//		direction = 1;
	//	} 
	//	else
	//	{
	//		direction = 2;
	//	}
	//	timeTransferState += time;
	//	if (timeTransferState > 40)
	//	{
	//		timeTransferState = 0;
	//		countFrame++;
	//		if (countFrame % 2 != 0)		//frame le
	//		{
	//			p_sprite = p_spriteBullet;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(8,9);
	//			} 
	//			else
	//			{
	//				p_sprite->setFrame(1,2);
	//			}
	//		}
	//		else								//frame chan
	//		{
	//			p_sprite = p_spriteBig;
	//			if (is_turn_left == true)
	//			{
	//				p_sprite->setFrame(8,9);
	//			} 
	//			else
	//			{
	//				p_sprite->setFrame(1,2);
	//			}
	//		}
	//		if (countFrame > 8)
	//		{				
	//			p_sprite = p_spriteBullet;
	//			changeState(MS_NORMAL);
	//		}
	//	}
	//	break;



	case MS_HASSTAR:
		processInput(input);
		move(obj_list, time, input);
		//UpdateNormal(staObjs, dynObjs, input, time);

		timeProtect += time;
		if (timeProtect > 40)
		{
			timeProtect = 0;
			countFrame++;
			if (countFrame % 2 != 0)		//frame le
			{
				/*if (isSmall == true)
				{
				p_sprite = p_spriteLight;
				} 
				else
				{
				p_sprite = p_spriteBig;
				}*/
			}
			else								//frame chan
			{
				/*if (isSmall == true)
				{
				p_sprite = p_spriteSmall;
				} 
				else
				{
				p_sprite = p_spriteBullet;
				}*/
			}
			if (countFrame > 243)
			{
				isHasStar = false;
				/*if (isSmall == true)
				{
				p_sprite = p_spriteSmall;
				} 
				else
				{
				if (isShoot == true)
				{
				p_sprite = p_spriteBullet;
				} 
				else
				{
				p_sprite = p_spriteBig;
				}
				}*/
				changeState(MS_NORMAL);
			}
		}
		break;



	}


	//sprite update
	p_sprite->update(time);

	if (isFight == true)
	{
		countFrameRod = p_sprite->getIndexFrame();
		if (countFrameRod >= 3)
		{
			p_sprite->setIndexFrame(0);
			isFight = false;
			p_sprite = p_sprite_go;
			countFrameRod = 0;
			if (is_turn_left)
			{
				p_sprite->setFrame(0,0);
			}
			else
				p_sprite->setFrame(7,7);
		}
	}

	if (is_active_axe == true)
	{
		countFrameRod = p_sprite->getIndexFrame();
		if (countFrameRod >= 2)
		{
			p_sprite->setIndexFrame(0);
			is_active_axe = false;
			p_sprite = p_sprite_go;
			countFrameRod = 0;
			if (is_turn_left)
			{
				p_sprite->setFrame(0,0);
			}
			else
				p_sprite->setFrame(7,7);
		}
	}

}


void Simon::move(list<Object*>* obj_list, float time, DxInput *p_input)
{

	timeBullet += time;		//time to bullet

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	Object *obj;
	list<Object*>::iterator i_obj;

	pos_x_old = pos_x;
	pos_y_old = pos_y;


	//Truc X
	float distanceX = veloc_x * time;
	pos_x += veloc_x * time;
	veloc_x = 0;

	if (pos_x <= 0)		//vi tri 0 cua map
	{
		pos_x = 0;
	}
	if (pos_x >= game_main->width_map - this->width)
	{
		pos_x = game_main->width_map - this->width;
	}


	//Truc Y
	float distanceY = 0;
	if (is_going_stair == false && game_main->game_state == GS_PLAYING)	//trang thai khong di len/xuong cau thang
	{
		if (isGravity == false)
		{
			accel_y = 0;
		}
		else
		{
			accel_y = GRAVITY;
		}

		veloc_y -= accel_y * time;
		distanceY = veloc_y * time;
		pos_y += veloc_y * time + 1.0f/2.0f * accel_y * time * time;
	}
	else
	{
		accel_y = 0;				
		pos_y += veloc_y * time;
		distanceY = veloc_y * time;
		veloc_y = 0;		
	}


	/*
	*	Kiem tra va cham voi cac doi tuong tinh
	*/
	for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
	{
		obj = *i_obj;

		if (obj->getObjectType() == OBJECT_ENEMY)
		{
			distanceX = distanceX - obj->getVelocX() * time;
			distanceY = distanceY - obj->getVelocY() * time;
		}

		Box box(this->pos_x_old,
			this->pos_y_old - this->height, 
			const_width,	//width
			height, 
			distanceX,
			distanceY  + 1.0f/2.0f * accel_y * time * time);

		Box block(obj->getPosX(),
			obj->getPosY() - obj->getHeight(),
			obj->getWidth(),
			obj->getHeight());

		if (obj->getObjectType() == OBJECT_ENEMY)
		{
			Box* box1 = new Box(obj->getPosXOld(),
				obj->getPosYOld() - obj->getHeight(),
				obj->getWidth(),
				obj->getHeight());
			block = *box1;
		}

		switch (obj->getIDObjType())	//obj stair
		{
		case 231:	//chan cau thang, len phai
			{
				Box* box1 = new Box(obj->getPosX(),
					obj->getPosY() - obj->getHeight(),
					2,
					2);
				block = *box1;

				//simon
				Box* box2 = new Box(this->pos_x_old,
					this->pos_y_old - this->height, 
					const_width, //width
					20, 
					distanceX,
					distanceY  + 1.0f/2.0f * accel_y * time * time);
				box = *box2;
			}
			break;

		case 241:	//chan cau thang, len trai
			{
				Box* box1 = new Box(obj->getPosX() + obj->getWidth() - 2,
					obj->getPosY() - obj->getHeight(),
					2,
					2);
				block = *box1;

				//simon
				Box* box2 = new Box(this->pos_x_old,
					this->pos_y_old - this->height, 
					const_width, //width
					20, 
					distanceX,
					distanceY  + 1.0f/2.0f * accel_y * time * time);
				box = *box2;
			}
			break;

		case 46:	//Piston
			{
				Box* box1 = new Box(obj->getPosX(),
					obj->getPosY() - obj->getHeight(),
					obj->getWidth(),
					obj->getHeight()/6);
				block = *box1;
			}
			break;

		case 50:
			//simon
			{
				Box* box2 = new Box(this->pos_x_old,
					this->pos_y_old - this->height, 
					const_width, //width
					5, 
					distanceX,
					distanceY  + 1.0f/2.0f * accel_y * time * time);
				box = *box2;
			}
			break;

		case 21:		//dynamic brick
			//simon
			{
				Box* box2 = new Box(this->pos_x_old,
					this->pos_y_old - this->height, 
					const_width, //width
					20, 
					distanceX,
					distanceY  + 1.0f/2.0f * accel_y * time * time);
				box = *box2;
			}
			break;
		}


		Box boxphase = Collision::GetSweptBroadphaseBox(box);	//simon
		if (Collision::AABBCheck(boxphase, block)) //co the co va cham
		{
			

			float normalx = 0;
			float normaly = 0;
			float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
			if (0 < collision_time && collision_time < 1)	//co va cham
			{
				isGravity = false;

				/*
				*	Xet Huong Cua Va Cham
				*/
				if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
				{
					//xet va cham voi gach, da,..
					if (obj->getObjectType() == OBJECT_LAND && is_going_stair == false)
					{
						if (this->pos_y_old - this->height <= obj->getPosY())
						{
							pos_x = pos_x_old;// + veloc_x * collision_time + 1.0f/2.0f * accel_x * collision_time *collision_time;
							veloc_x = 0;						
							accel_x = 0;
						}
						
					}

				} 
				else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
				{
					//xet va cham voi gach, da,..
					if (obj->getObjectType() == OBJECT_LAND && is_going_stair == false)
					{
						if (this->pos_y_old - this->height <= obj->getPosY())
						{
							pos_x = pos_x_old;// + veloc_x * collision_time + 1.0f/2.0f * accel_x * collision_time *collision_time;
							veloc_x = 0;						
							accel_x = 0;
						}
						
					}

					

				}
				else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
				{

					//if (obj->getObjectType() == OBJECT_LAND && is_going_stair == false)
					//{
					//	/*pos_y = pos_y_old + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;
					//	accel_y = 0;
					//	veloc_y = 0;*/
					//}
					

				}
				else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
				{
					/*
					*	Kiem tra va cham voi cac gach, dat,...
					*/
					if (obj->getObjectType() == OBJECT_LAND && is_going_stair == false)
					{
						pos_y = pos_y_old;// + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;// + 0.11;
						is_jump = false;
						veloc_y = 0;
					}

					

				}//Ket thuc xet va cham theo huong


				if (this->getObjState() == MS_NORMAL)
				{
					switch (obj->getObjState())
					{
					//case 14:		//red bat
					//	if (obj->getObjState() == ES_ACTIVING)
					//	{
					//		if (this->getObjState() == MS_NORMAL)
					//		{
					//			this->is_left_die = obj->getWay();

					//			int blood = this->getBlood();
					//			blood--;
					//			this->setBlood(blood);
					//			if (this->getBlood() == 0)
					//			{
					//				this->changeState(MS_DIE);		//simon die
					//			}
					//			else
					//			{
					//				if (is_going_stair == true)		//len/xuong cau thang
					//					this->changeState(MS_LIGHT);
					//				else
					//					this->changeState(MS_BLOOD_LOOS);		//simon 
					//			}
					//		}
					//	}
					//	break;
					}
				}



			}//Ket thuc co va cham
			else
			{
				isGravity = true;

			}

			if (this->obj_state == MS_NORMAL)
			{
				//va cham voi obj
				switch (obj->getIDObjType())
				{
				case 21:
					this->veloc_x = obj->getVelocX();
					break;

				case 22:	//land
					goingStairOut();
					break;


				case 231:	//stair up right
					is_up_left = false;

					x_temp = obj->getPosX();
					y_temp = obj->getPosY();

					if (isUp == 1)	//di len cau thang
					{
						if (is_turn_left == false)
						{
							pos_x = x_temp - this->getWidth()/2;
							is_going_stair = true;					//flag len cau thang
							is_until_go_out_stair = true;
						}
					}
					else if (isUp == -1)	//di xuong cau thang
					{
						is_until_go_out_stair = false;
					}
					break;

				case 232:	//stair	
					is_up_left = false;
					if (isUp == 1)	//di len
					{
						if (is_until_go_out_stair == true)
						{
							this->pos_x += 1;
							this->pos_y += 1;
							if (this->pos_y > obj->getPosY() + (this->getHeight() - obj->getHeight()))
							{
								if (this->pos_x > obj->getPosX() - this->getWidth())
								{
									is_until_go_out_stair = false;
								}								
							}
						}
						goingStairOut();
					}
					break;

				case 233:	//stair	
					is_up_left = false;
					x_temp = obj->getPosX();
					y_temp = obj->getPosY() - obj->getHeight();
					if (isUp == -1)
					{
						if (is_turn_left == true)
						{
							is_until_go_out_stair = true;
							is_going_stair = true;	
							if (pos_y > y_temp + 48)
							{
								pos_y -= 1;	
							}
						}
					}
					break;


				case 241:	//stair	up left
					is_up_left = true;
					x_temp = obj->getPosX();
					y_temp = obj->getPosY();

					if (isUp == 1)
					{						
						if (is_going_stair == false)
						{
							if (is_turn_left == true)
							{
								pos_x = x_temp + obj->getWidth()/2;// - this->getWidth()/2;
								is_going_stair = true; //flag len cau thang

								is_until_go_out_stair = true;

							}
						}

					}

					if (isUp == -1)
					{
						is_until_go_out_stair = false;
					}
					break;

				case 242:	//stair	
					is_up_left = true;

					if (isUp == 1)	//di len
					{

						if (is_until_go_out_stair == true)
						{
							this->pos_x -= 1;
							this->pos_y += 1;

							if (this->pos_y > obj->getPosY() + (this->getHeight() - obj->getHeight()))
							{
								if (this->pos_x > obj->getPosX() - this->getWidth())
								{
									is_until_go_out_stair = false;
								}								
							}
						}

						goingStairOut();

					}
					break;

				case 243:	//stair	
					is_up_left = true;

					x_temp = obj->getPosX();
					y_temp = obj->getPosY() - obj->getHeight();
					if (isUp == -1)
					{
						if (is_turn_left == false)
						{

							is_until_go_out_stair = true;
							is_going_stair = true;	

							if (pos_y > y_temp + 48)
							{
								pos_y -= 1;	
							}
						}
					}
					break;		

				case 31:	//1 heart - candle
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						game_main->num_heart += 1;
						game_main->scoreArr[HEART]->Update(game_main->num_heart);					
						game_main->dxAudio->PlaySoundW(game_main->eat_item);

					}				
					break;

				case 32:	//5 heart - candle
				case 321:	//5 heart - pot
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						game_main->num_heart += 5;
						game_main->scoreArr[HEART]->Update(game_main->num_heart);					
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}				
					break;

				case 33:	//whip pot
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(MS_LIGHT);
						this->rod_version++;
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}
					break;

				case 34:	//dagger - pot
				case 341:	//dagger - candle
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(MS_LIGHT);
						this->weapon_version = WEAPON_DAGGER;
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}
					break;

				case 35:	//400 pts - candle
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						//game_main->num_score += 400;
						//game_main->scoreArr[SCORE]->Update(game_main->num_score);
						game_main->InfoScore(400, obj->getPosX(), obj->getPosY());
						game_main->scoreArr[SCORE]->Update(game_main->num_score);
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}				
					break;

				case 36:	//700 pts - candle
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						/*game_main->num_score += 700;
						game_main->scoreArr[SCORE]->Update(game_main->num_score);*/
						game_main->InfoScore(700, obj->getPosX(), obj->getPosY());
						game_main->scoreArr[SCORE]->Update(game_main->num_score);
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}				
					break;

				case 37:	//1000 pts - candle
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						/*game_main->num_score += 1000;
						game_main->scoreArr[SCORE]->Update(game_main->num_score);*/
						game_main->InfoScore(1000, obj->getPosX(), obj->getPosY());
						game_main->scoreArr[SCORE]->Update(game_main->num_score);
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}				
					break;

				case 38:	//Holy water
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(MS_LIGHT);
						this->weapon_version = WEAPON_HOLY_WATER;
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}
					break;

				case 39:	//Holy cross
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);

						//cac enemy nam trong camera bi chet
						Object* obj_enemy;
						list<Object*>::iterator i_obj;
						for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
						{
							obj_enemy = *i_obj;						
							switch (obj_enemy->getIDObjType())
							{
							case 11:
							case 121:
							case 122:
							case 13:
							case 14:
							case 15:
								obj_enemy->changeState(ES_DIED);
								break;
							}
						}

						this->changeState(MS_LIGHT);

						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}
					break;

				case 41:	//Food
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(MS_LIGHT);
						this->blood += 2;
						if (this->blood >= 8)
						{
							this->blood = 8;
						}
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}
					break;

				case 42:	//Axe
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(MS_LIGHT);
						this->weapon_version = WEAPON_AXE;
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}
					break;

				case 44:	//Orb
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(MS_LIGHT);
						this->blood = 8;
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
						game_main->ChangeState(GS_NEXT_STAGE);
					}
					break;

				case 45:	//Boomerang
					if (obj->getObjState() == IS_ACTIVING)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(MS_LIGHT);
						this->weapon_version = WEAPON_BOOMERANG;
						game_main->dxAudio->PlaySoundW(game_main->eat_item);
					}
					break;

				case 46:	//Piston
					if (this->getObjState() == MS_NORMAL)
					{
						int blood = this->getBlood();
						blood--;
						this->setBlood(blood);
						if (this->getBlood() == 0)
						{
							this->changeState(MS_DIE);		//simon die
						}
						else
						{
							this->changeState(MS_LIGHT);
							//if (is_going_stair == true)		//len/xuong cau thang
							//	
							//else
							//	this->changeState(MS_BLOOD_LOOS);		//simon 
						}
					}
					break;

				case 50:	//check point
					game_main->ChangeState(GS_NEXT_STAGE);
					isGravity = false;
					veloc_y = 0;
					accel_y = 0;
					break;

				case 11:		//ghost
				case 121:		//dog left
				case 122:		//dog right
				

				case 15:		//red dragon
				case 16:		//stone knight
					if (obj->getObjState() == ES_ACTIVING)
					{
						if (this->getObjState() == MS_NORMAL)
						{
							this->is_left_die = obj->getWay();

							int blood = this->getBlood();
							blood--;
							this->setBlood(blood);
							if (this->getBlood() == 0)
							{
								this->changeState(MS_DIE);		//simon die
							}
							else
							{
								if (is_going_stair == true)		//len/xuong cau thang
									this->changeState(MS_LIGHT);
								else
									this->changeState(MS_BLOOD_LOOS);		//simon 
							}
						}
					}
					break;

				case 13:		//bat boss
				case 18:
					if (obj->getObjState() == ES_ACTIVING)
					{
						if (this->getObjState() == MS_NORMAL)
						{
							this->is_left_die = obj->getWay();

							int blood = this->getBlood();
							blood--;
							this->setBlood(blood);
							if (this->getBlood() == 0)
							{
								this->changeState(MS_DIE);		//simon die
							}
							else
							{
								this->changeState(MS_BLOOD_LOOS);		//simon mat mau
							}
						}
					}
					break;


				}	//end switch

			}

		}//Ket thuc co the co va cham
		else
		{
			isGravity = true;//new
		}

	}//end for





	pos_x_old = pos_x;
	pos_y_old = pos_y;

	if (pos_y <= 0)	//simon roi xuong ho
	{
		veloc_y = 0;
		accel_y = 0;
		changeState(MS_DIE);

	}



	//Ngan mario rot xuong ma van con nhay duoc
	if (veloc_y < 0)
	{
		is_jump = true;
	}

} 

void Simon::moveDieState(list<Object*>* obj_list, float time, DxInput *p_input)
{

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	Object *obj;
	list<Object*>::iterator i_obj;

	pos_x_old = pos_x;
	pos_y_old = pos_y;


	//Truc X
	float distanceX = veloc_x * time;
	pos_x += veloc_x * time;


	if (pos_x <= 0)		//vi tri 0 cua map
	{
		pos_x = 0;
	}
	if (pos_x >= game_main->width_map - this->width)
	{
		pos_x = game_main->width_map - this->width;
	}


	//Truc Y
	float distanceY = 0;
	if (game_main->game_state == GS_PLAYING)	//trang thai khong di len/xuong cau thang
	{
		if (isGravity == false)
		{
			accel_y = 0;
		}
		else
		{
			accel_y = GRAVITY;
		}

		veloc_y -= accel_y * time;
		distanceY = veloc_y * time;
		pos_y += veloc_y * time + 1.0f/2.0f * accel_y * time * time;
	}
	else
	{
		accel_y = 0;				
		pos_y += veloc_y * time;
		distanceY = veloc_y * time;
		veloc_y = 0;		
	}


	/*
	*	Kiem tra va cham voi cac doi tuong tinh
	*/
	for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
	{
		obj = *i_obj;

		Box box(this->pos_x_old,
			this->pos_y_old - this->height, 
			width, 
			height, 
			distanceX,
			distanceY  + 1.0f/2.0f * accel_y * time * time);

		Box block(obj->getPosX(),
			obj->getPosY() - obj->getHeight(),
			obj->getWidth(),
			obj->getHeight());


		Box boxphase = Collision::GetSweptBroadphaseBox(box);
		if (Collision::AABBCheck(boxphase, block)) //co the co va cham
		{
			float normalx = 0;
			float normaly = 0;
			float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
			if (0 < collision_time && collision_time < 1)	//co va cham
			{
				isGravity = false;

				/*
				*	Xet Huong Cua Va Cham
				*/
				if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
				{
					//xet va cham voi gach, da,..
					if (obj->getObjectType() == OBJECT_LAND)
					{
						if (this->pos_y_old - this->height < obj->getPosY())
						{
							pos_x = pos_x_old;// + veloc_x * collision_time + 1.0f/2.0f * accel_x * collision_time *collision_time;
							veloc_x = 0;						
							accel_x = 0;
						}

					}


				} 
				else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
				{
					//xet va cham voi gach, da,..
					if (obj->getObjectType() == OBJECT_LAND)
					{

						if (this->pos_y_old - this->height < obj->getPosY())
						{
							pos_x = pos_x_old;// + veloc_x * collision_time + 1.0f/2.0f * accel_x * collision_time *collision_time;
							veloc_x = 0;						
							accel_x = 0;
						}

					}


				}
				else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
				{

				}
				else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
				{
					/*
					*	Kiem tra va cham voi cac gach, dat,...
					*/
					if (obj->getObjectType() == OBJECT_LAND)
					{
						pos_y = pos_y_old + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;// + 0.11;
						is_jump = false;
						veloc_y = 0;
						changeState(MS_PROTECT);
					}


				}//Ket thuc xet va cham theo huong


			}//Ket thuc co va cham
			else
			{
				isGravity = true;

			}


		}//Ket thuc co the co va cham
		else
		{
			isGravity = true;//new
		}

	}//end for

	pos_x_old = pos_x;
	pos_y_old = pos_y;

	if (pos_y <= 0)		///roi xuong ho, cuoi man hinh
	{
		changeState(MS_REMOVE);

		if (game_main->num_life == 0)
		{
			game_main->ChangeState(GS_GAMEOVER);
		}
		else
		{
			game_main->ChangeState(GS_REPLAY);
		}

	}

}

/**--------------------------------------------------------
*Description : Render mario
*Method name : Render
*Parameters :  
*Return value :void
--------------------------------------------------------**/
void Simon::render(int cameraX, int cameraY)
{
	switch (this->obj_state)
	{
	case MS_NORMAL:
		{
			if (isFight == true || is_active_axe == true)
			{
				if (is_turn_left == true)
				{
					p_sprite->render((float)pos_x-12, (float)pos_y, cameraX, cameraY);	
				}
				else
				{
					p_sprite->render((float)pos_x-16, (float)pos_y, cameraX, cameraY);
				}
			}
			else
			{
				p_sprite->render((float)pos_x, (float)pos_y, cameraX, cameraY);
			}

			if (p_rod != NULL)// && isFight == true)
			{
				p_rod->render(cameraX, cameraY);
			}

		}
		break;

	case MS_BLOOD_LOOS:
	case MS_DIE:
	case MS_LIGHT:
	case MS_PROTECT:
		{
			if (isFight == true || is_active_axe == true)
			{
				if (is_turn_left == true)
				{
					p_sprite->render((float)pos_x-12, (float)pos_y, cameraX, cameraY);	
				}
				else
				{
					p_sprite->render((float)pos_x-16, (float)pos_y, cameraX, cameraY);
				}
			}
			else
			{
				p_sprite->render((float)pos_x, (float)pos_y, cameraX, cameraY);
			}
		}
		break;

	default:
		break;
	}

	if (p_axe != NULL)// && isFight == true)
	{
		p_axe->render(cameraX, cameraY);
	}
	if (p_dagger != NULL)
	{
		p_dagger->render(cameraX, cameraY);
	}
	if (p_holy_water != NULL)
	{
		p_holy_water->render(cameraX, cameraY);
	}
	if (p_boomerang != NULL)
	{
		p_boomerang->render(cameraX, cameraY);
	}
}



/**--------------------------------------------------------
*Description : mario nhay sang trai
*Method name : TurnLeft
*Parameters :  
*Return value :void
--------------------------------------------------------**/
void Simon::turnLeft()
{
	if (is_going_stair == false)// && is_coll_stair == false)
	{
		is_turn_left = true;
		if (is_jump == true)
		{
			p_sprite->setFrame(3, 3);		 
		}
		else
		{
			p_sprite->setFrame(0, 3);			 
		}
		veloc_x = - SIMON_VX;		
	}
}


/**--------------------------------------------------------
*Description : mario nhay sang phai
*Method name : TurnRight
*Parameters :  
*Return value :void
--------------------------------------------------------**/
void Simon::turnRight()
{	
	if (is_going_stair == false)// && is_coll_stair == false)
	{
		is_turn_left = false;
		if (is_jump == true)
		{
			p_sprite->setFrame(4, 4);

		}
		else
		{
			p_sprite->setFrame(5, 8);

		}
		veloc_x = SIMON_VX;
	}
}

/**--------------------------------------------------------
*Description : mario di len cau thang
*Method name : TurnUp
*Parameters :  
*Return value :void
--------------------------------------------------------**/
void Simon::turnUp()
{	
	if (is_going_stair == true)	 
	{
		if (is_turn_left == false)	//go right
		{
			is_turn_left = false;

			p_sprite = p_sprite_stair;
			p_sprite->setFrame(2,4);

			veloc_x = SIMON_VX;
			veloc_y = SIMON_VX;//SIMON_VY/10;


		}
		else
		{
			is_turn_left = true;
			p_sprite = p_sprite_stair;
			p_sprite->setFrame(4,6);

			veloc_x = -SIMON_VX;
			veloc_y = SIMON_VX;
		}
	}
}

/**--------------------------------------------------------
*Description : mario di xuong cau thang
*Method name : TurnDown
*Parameters :  
*Return value :void
--------------------------------------------------------**/
void Simon::turnDown()
{

	if (is_going_stair == true)// || is_coll_stair == true)
	{
		if (is_turn_left == true)
		{
			is_turn_left = true;
			p_sprite = p_sprite_stair;
			p_sprite->setFrame(0,2);
			//p_sprite->setIndex(0);
			veloc_x = - SIMON_VX;
			veloc_y = - SIMON_VX;
		}
		else
		{
			is_turn_left = false;
			p_sprite = p_sprite_stair;
			p_sprite->setFrame(6,8);
			//p_sprite->setIndex(0);
			veloc_x =  SIMON_VX;
			veloc_y = -SIMON_VX;
		}
	}

}

/**--------------------------------------------------------
*Description : mario nhay len
*Method name : Jump
*Parameters :  
*Return value :void
--------------------------------------------------------**/
void Simon::jump()
{
	if(!is_jump && is_going_stair == false)
	{
		is_jump = true;
		if (is_turn_left == true)
		{
			p_sprite->setFrame(3, 3);

		}
		else
		{
			p_sprite->setFrame(4, 4);

		}
		veloc_y = SIMON_VY;
	}
}


/**--------------------------------------------------------
*Description : Mario tro ve ban dau
*Method name : Reset
*Parameters :  
*Return value :void
--------------------------------------------------------**/
void Simon::reset()
{
	if (is_turn_left == true)
	{
		if (is_going_stair == false)
		{
			if (is_jump == true)
			{
				if (veloc_y < 0)
				{
					p_sprite->setFrame(0, 0);
				}
			}
			else 
			{
				p_sprite->setFrame(0, 0);

			}
		}
		else
		{
			if (is_direction_stair == 1)		//len trai
			{
				p_sprite->setFrame(4,4);
			}
			else if (is_direction_stair == 3)	//xuong trai
			{
				p_sprite->setFrame(0,0);
			}

		}
	}
	else
	{
		if (is_going_stair == false)
		{
			if (is_jump == true)
			{
				if (veloc_y < 0)
				{
					p_sprite->setFrame(7, 7);
				}
			}
			else
			{
				p_sprite->setFrame(7, 7);
			}
		}
		else
		{
			if (is_direction_stair == 2)		//len phai
			{
				p_sprite->setFrame(2,2);
			}
			else if (is_direction_stair == 4)	//xuong phai
			{
				p_sprite->setFrame(6,6);
			}
		}
	}
}

void Simon::fight()
{
	if (isFight == false)
	{
		isFight = true;
		if (is_turn_left == true)
		{					
			p_sprite = p_sprite_player_rod;

			p_sprite->setFrame(0,3);	//new
			p_sprite->setIndex(0);

			if (rod_version == 1)
			{
				p_rod = new Rod(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY(), true, 1);	
			}
			else if (rod_version == 2)
			{
				p_rod = new Rod(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY(), true, 2);
			}
			else if (rod_version >= 3)
			{
				p_rod = new Rod(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY(), true, 3);
			}
		}
		else
		{
			p_sprite = p_sprite_player_rod;

			p_sprite->setFrame(3,6);	//new
			p_sprite->setIndex(3);//3

			if (rod_version == 1)
			{
				p_rod = new Rod(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY(), false, 1);				
			}	
			else if (rod_version == 2)
			{
				p_rod = new Rod(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY(), false, 2);
			}
			else if (rod_version >= 3)
			{
				p_rod = new Rod(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY(), false, 3);
			}
		}
		p_sprite->setIndexFrame(0);


	}

}


void Simon::activeAxe()
{
	if (is_active_axe == false && weapon_version != WEAPON_NONE && game_main->num_heart != 0)
	{
		game_main->num_heart--;
		if (game_main->num_heart <= 0)
		{
			game_main->num_heart = 0;
		}
		game_main->scoreArr[HEART]->Update(game_main->num_heart);

		is_active_axe = true;
		if (is_turn_left == true)
		{					
			p_sprite = p_sprite_player_rod;
			p_sprite->setFrame(0,2);	//new
			p_sprite->setIndex(0);

			switch (weapon_version)
			{
			case WEAPON_AXE:
				p_axe = new Axe(d3ddv, backBuffer, spriteHandler, this->getPosX() + this->width/2, this->getPosY()-16, true);
				break;

			case WEAPON_DAGGER:
				p_dagger = new Dagger(d3ddv, backBuffer, spriteHandler, this->getPosX() + this->width/2, this->getPosY()-16, true);
				break;

			case WEAPON_HOLY_WATER:
				p_holy_water = new HolyWater(d3ddv, backBuffer, spriteHandler, this->getPosX() + this->width/2, this->getPosY()-16, true);
				break;

			case WEAPON_BOOMERANG:
				p_boomerang = new Boomerang(d3ddv, backBuffer, spriteHandler, this->getPosX() + this->width/2, this->getPosY()-16, true);
				break;
			}

		}
		else
		{
			p_sprite = p_sprite_player_rod;
			p_sprite->setFrame(3,5);	//new
			p_sprite->setIndex(3);

			switch (weapon_version)
			{
			case WEAPON_AXE:
				p_axe = new Axe(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY(), false);
				break;

			case WEAPON_DAGGER:
				p_dagger = new Dagger(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY()-16, false);
				break;

			case WEAPON_HOLY_WATER:
				p_holy_water = new HolyWater(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY()-16, false);
				break;

			case WEAPON_BOOMERANG:
				p_boomerang = new Boomerang(d3ddv, backBuffer, spriteHandler, this->getPosX(), this->getPosY()-16, false);
				break;
			}

		}
	}
}

void Simon::processInput(DxInput *p_input)
{
	//NOTE: neu khi nhan phim danh roi nhung k an, chinh lai dik_... j do, khac phim ban dau.
	//truong hop bi nay do clean project.



	//Dieu khien nhay
	int temp = p_input->GetKeyDown();
	switch (temp)
	{
	case DIK_F:
		{
			this->jump();
			//game_main->dxAudio->PlaySoundW(game_main->jumpSound);		
			trace(L"Ban dang nhan phim F!");
		}		
		break;

	case DIK_P:
		{
			if (isFight == false)
			{
				this->fight();
				this->accel_x = 0;
				this->veloc_x = 0;
			}
			trace(L"Ban dang nhan phim D!");
		}
		break;

	case DIK_E:
		{
			if (is_active_axe == false)
			{
				this->activeAxe();
				this->accel_x = 0;
				this->veloc_x = 0;
			}
			trace(L"Ban dang nhan phim E!");
		}
		break;

	default:
		break;
	}





	//State
	//Nhan phim va giu
	if (p_input->IsKeyDown(DIK_D))
	{
		if (isFight == false)
		{
			this->fight();
			this->accel_x = 0;
			this->veloc_x = 0;
			game_main->dxAudio->PlaySoundW(game_main->rod_sound);
		}
		trace(L"Ban dang nhan phim D!");
	}
	else
		if (p_input->IsKeyDown(DIK_UP))
		{
			isUp = 1;

			if (is_going_stair == true)
			{
				if (is_up_left == false)	//len sang phai
				{
					is_direction_stair = 2;
					if (is_turn_left == true)
					{
						is_turn_left = false;
					}

					turnUp();
				}
				//len sang trai
				else	
				{
					is_direction_stair = 1;
					if (is_turn_left == false)
					{
						is_turn_left = true;
					}

					turnUp();
				}
			}
		} 
		else 
			if (p_input->IsKeyDown(DIK_DOWN))
			{		
				isUp = -1;
				if (is_going_stair == true)
				{

					if (is_up_left == false)	//xuong sang trai
					{
						is_direction_stair = 3;
						if (is_turn_left == false)
						{
							is_turn_left = true;
						}

						turnDown();		 
					}
					//xuong sang phai
					else
					{
						is_direction_stair = 4;
						if (is_turn_left == true)
						{
							is_turn_left = false;
						}
						turnDown();		
					}
				}
			}
			else if (p_input->IsKeyDown(DIK_SPACE))
			{ 	

				if (is_turn_left == true)
				{
					p_sprite->setFrame(3,4);
				}
				else
				{
					p_sprite->setFrame(4,5);
				}		
				trace(L"Ban dang nhan phim Down!");

			}
			else if (p_input->IsKeyDown(DIK_RIGHT) && isFight == false)
			{
				isUp = 0;

				this->turnRight();
				trace(L"Ban dang nhan phim Right!");
			}	
			else if (p_input->IsKeyDown(DIK_LEFT) && isFight == false)
			{
				isUp = 0;

				this->turnLeft();
				trace(L"Ban dang nhan phim Left!");
			}
			else
			{
				if (isFight == false && is_active_axe == false)
				{
					this->reset();
				}


			}

}

D3DXVECTOR2 Simon::getPosition()
{
	D3DXVECTOR2 position;
	position.x = pos_x;
	position.y = pos_y;
	return position;
}


void Simon::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
	case MS_NORMAL:
		pos_y += 2;
		if (is_turn_left == true)
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(0, 1);
			p_sprite->setIndex(0);
		}
		else
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(7, 8);
			p_sprite->setIndex(7);
		}
		break;

	case MS_BLOOD_LOOS:				//mat mau
		if (blood == 0)
		{
			this->changeState(MS_DIE);
		}
		//simon
		if (is_left_die == true)	//left
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(17, 18);
			p_sprite->setIndex(17);
			veloc_x = -SIMON_VX;
		}
		else if (is_left_die == false)	//right
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(16, 17);
			p_sprite->setIndex(16);
			veloc_x = SIMON_VX;
		}

		veloc_y = SIMON_VY;
		accel_y = GRAVITY;

		if (p_rod != NULL)
		{
			p_rod->changeState(IS_REMOVE);
		}
		if (p_axe != NULL)
		{
			p_axe->changeState(IS_REMOVE);
		}
		break;

	case MS_DIE:
		timeProtect = 0;
		countFrame = 0;
		/*if (blood == 0)
		{
		game_main->ChangeState(GS_REPLAY);
		}*/
		//simon
		if (is_left_die == true)	//left
		{
			p_sprite = p_sprite_die;
			p_sprite->setFrame(0, 1);
			p_sprite->setIndex(0);
			//veloc_x = -SIMON_VX;
		}
		else if (is_left_die == false)	//right
		{
			p_sprite = p_sprite_die;
			p_sprite->setFrame(1, 2);
			p_sprite->setIndex(1);
			//veloc_x = SIMON_VX;
		}

		//veloc_y = SIMON_VY;
		//accel_y = GRAVITY;

		if (p_rod != NULL)
		{
			p_rod->changeState(IS_REMOVE);
		}
		if (p_axe != NULL)
		{
			p_axe->changeState(IS_REMOVE);
		}
		break;

	case MS_PROTECT:
		timeProtect = 0;
		countFrame = 0;
		if (is_turn_left == true)
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(8,12);
		}
		else
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(12,16);
		}
		break;

	case MS_LIGHT:
		timeProtect = 0;
		countFrame = 0;
		if (is_turn_left == true)
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(8,12);
		}
		else
		{
			p_sprite = p_sprite_go;
			p_sprite->setFrame(12,16);
		}
		break;

		//////////////////////////////////////////////////////////////////////////
	//case MS_CREPTPINE:			//chui cong
	//	accel_y = GRAVITY/10;
	//	break;


	case MS_REMOVE:
		break;

		/*case MS_SMALLTOBIG:
		timeTransferState = 0;
		countFrame = 0;
		break;

		case MS_BIGTOSMALL:
		timeTransferState = 0;
		countFrame = 0;
		break;

		case MS_BIGTOBULLET:
		timeTransferState = 0;
		countFrame = 0;
		break;

		case MS_BULLETTOSMALL:
		timeTransferState = 0;
		countFrame = 0;
		break;*/

	case MS_HASSTAR:
		timeProtect = 0;
		countFrame = 0;
		break;


	}
}


void Simon::goingStairOut()
{
	if (is_until_go_out_stair == false)
	{
		if (is_going_stair == true)
		{
			is_going_stair = false;
			p_sprite = p_sprite_go;

		}
	}
}